using FITClub.Episode1.Controller;
using FITClub.TextureMenu;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace LProject.XNA.RemoteController
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TouchGamePadDisplay : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public TouchGamePadDisplay(Game game, ContentManager content, TouchController controller)
            : base(game)
        {
            this.Regions = controller.Regions;
            this.controller = controller;
            Color AttackColor = Color.FromNonPremultiplied(214, 0, 0, 80);
            Color DPadColor = Color.FromNonPremultiplied(0, 107, 214, 80);
            this.HighlightColor = Color.FromNonPremultiplied(255, 138, 20, 100);
            Color DefenceColor = Color.FromNonPremultiplied(128, 214, 0, 80);

            this.labels = new string[] { "Up", "Left", "Down", "Right", "ATK", "DEF" };

            this.ButtonColors = new Color[] { DPadColor, DPadColor, DPadColor, DPadColor, AttackColor, DefenceColor, DPadColor, DPadColor };

            circle = content.Load<Texture2D>("controller\\touch_circle");
            this.circleArrows = content.Load<Texture2D>("controller\\touch_circle_arrows");
            this.controllerTextures = new Texture2D[] {
                content.Load<Texture2D>("controller\\touch_up"),
                content.Load<Texture2D>("controller\\touch_left"),
                content.Load<Texture2D>("controller\\touch_down"),
                content.Load<Texture2D>("controller\\touch_right"),
                circle,circle
            };
            this.extraTextures = new Texture2D[]{
                content.Load<Texture2D>("controller\\touch_upleft"),
                content.Load<Texture2D>("controller\\touch_upright"),
            };
        }

        private Texture2D circle;
        private Texture2D circleArrows;

        public Texture2D BorderTexture { get; protected set; }

        public Rectangle[] Regions { get; protected set; }

        public Color[] ButtonColors { get; protected set; }

        public Color HighlightColor { get; protected set; }

        protected SpriteBatch spriteBatch;
        protected Texture2D[] controllerTextures;
        protected TouchController controller;
        protected string[] labels;
        protected Texture2D[] extraTextures;
        protected Rectangle border;

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            //Font = this.Game.Content.Load<SpriteFont>("GamepadFont");
            Color c = new Color(50, 65, 160, 120);
            this.BorderTexture = MenuHelper.CreateFillTexture(c);
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            border = new Rectangle(controller.ThumbstickDefault.X-5 , controller.ThumbstickDefault.Y-5 , controller.ThumbstickDefault.Width+10 , controller.ThumbstickDefault.Height+10);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin();
            VirtualGamepadState state = this.controller.GetState();
            if (this.controller.UseArrows)
            {
                for (int i = 0; i < this.Regions.Length; i++)
                {
                    spriteBatch.Draw(this.controllerTextures[i], this.Regions[i], (state[i]) ? this.HighlightColor : this.ButtonColors[i]);
                }
                spriteBatch.Draw(this.extraTextures[0], this.controller.UpLeftRegion, (state[0] && state[1]) ? this.HighlightColor : this.ButtonColors[this.Regions.Length]);
                spriteBatch.Draw(this.extraTextures[1], this.controller.UpRightRegion, (state[0] && state[3]) ? this.HighlightColor : this.ButtonColors[this.Regions.Length + 1]);
            }
            else
            {                                
                MenuHelper.DrawBorder(spriteBatch, this.BorderTexture, border, Color.White, 4);
                
                spriteBatch.Draw(circleArrows, this.controller.Thumbstick, (this.controller.ThumbstickHit) ? this.HighlightColor : this.ButtonColors[1]);

                spriteBatch.Draw(circle, this.controller.Attack, (state[4]) ? this.HighlightColor : this.ButtonColors[4]);
                spriteBatch.Draw(circle, this.controller.Defence, (state[5]) ? this.HighlightColor : this.ButtonColors[5]);
            }
            spriteBatch.End();
        }
    }
}